package com.intramirror.wd.service.security.model.token;

import com.intramirror.wd.service.security.config.TokenProperties;
import com.intramirror.wd.service.security.exceptions.ExpiredTokenException;
import io.jsonwebtoken.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;

import java.util.HashMap;
import java.util.Map;

public class RawAccessToken implements Token {
	
    private static Logger logger = LoggerFactory.getLogger(RawAccessToken.class);

    private String token;
    
    public RawAccessToken(String token) {
        this.token = token;
    }

    /**
     * 分析并且验证Token是否有效
     * 
     * @throws BadCredentialsException  如果验证请求被拒绝，则因为凭据无效 <br> 对于要抛出的异常，它意味着该帐户既不锁定也不禁用。 <br> 
     * @throws ExpiredTokenException 过期的Token
     * 
     */
    public Jws<Claims> parseClaims(String signingKey) {
    	try {
            Jws<Claims> jws = Jwts.parser().setSigningKey(signingKey).parseClaimsJws(this.token);
            AccessTokenTimeCheck.checkTime(this.token);
            return jws;
        } catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException | SignatureException ex) {
            logger.error("Invalid Token", ex);
            throw new BadCredentialsException("Invalid token: ", ex);
        } catch (ExpiredJwtException expiredEx) {
            logger.info("Token is expired", expiredEx);
            throw new ExpiredTokenException(this, "Login was timeout! Please login again!", expiredEx);
        }
    }

    @Override
    public String getToken() {
        return token;
    }
}
